package com.social.instadownloader.screen;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.social.instadownloader.R;
import com.social.instadownloader.model.InstaFileModel;
import com.social.instadownloader.screen.common.InstaFragmentScreen;
import com.social.instadownloader.screen.common.MediaAdapter;
import com.social.instadownloader.screen.common.MediaAdapter.OnMediaItemClickListener;
import com.social.instadownloader.util.DialogUtil;
import com.social.instadownloader.util.DialogUtil.OnDialogListener;
import com.social.instadownloader.util.InstaFileManager;
import com.social.instadownloader.util.Launcher;

public class HistoryScreen extends InstaFragmentScreen{

	private MediaAdapter	mAdapter;
	private ListView		mListView;
	private ArrayList<InstaFileModel> mDatas;
	public static boolean NEED_RELOAD;
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.downloaded_screen, container, false);
		mListView = (ListView) v.findViewById(R.id.listDownloaded);
		showData();
		NEED_RELOAD = false;
		return v;
	}
	
	public void showData(){
		mDatas = InstaFileManager.getListOfInstaFiles();
		mAdapter = new MediaAdapter(getActivity(), mDatas, new OnMediaItemClickListener() {
			
			@Override
			public void onShareClick(int pos) {
				shareInstagramImage(getActivity(), mDatas.get(pos).absolutePath, "");
			}
			
			@Override
			public void onOptionClick(int pos) {
				deleteItem(pos);
			}
		});
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Launcher.openFile(getActivity(), mAdapter.getItem(pos).absolutePath);
			}
			
		});
	}
	
	private void deleteItem(final int index){
		DialogUtil.showConfirmDialog(getActivity(), "Confirm", "Do you want delete item?", new OnDialogListener() {
			
			@Override
			public void onPositive(String result) {
				InstaFileModel object = mAdapter.getItem(index);
				File file = new File(object.absolutePath);
				if(file.exists()){
					file.delete();
				}
				mDatas.remove(index);
				mAdapter.notifyDataSetChanged();
			}
			
			@Override
			public void onCancel() {
				
			}
		});
	}

	private void shareInstagramImage(Context paramContext, String absolutePath, String url)
	  {
	    if(("".equals(absolutePath))){
	    	return;
	    }
	    Intent localIntent = new Intent("android.intent.action.SEND");
	    localIntent.setType("image/jpeg");
	    localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
	    localIntent.putExtra("android.intent.extra.SUBJECT", "Let's check out this interesting Instagram post");
	    localIntent.putExtra("android.intent.extra.TEXT", url + " #InstaDownloader");
	    localIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(new File(absolutePath)));
	    paramContext.startActivity(Intent.createChooser(localIntent, "Share image via"));
	  }
	
	@Override
	public void onResume() {
		if (NEED_RELOAD) {
			showData();
		}
		super.onResume();
	}
	
	@Override
	public void onDestroyView() {
		NEED_RELOAD = false;
		super.onDestroyView();
	}
	
	@Override
	public void onLeftIconClick(View v) {
		super.onLeftIconClick(v);
		showData();
	}
	
	@Override
	public String getScreenName() {
		return "Downloaded";
	}

}
